package cn.kgm.makeGodV2.ruleEngine;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-9-24 10:29
 * @Description: cn.kgm.makeGodV2.ruleEngine.ItemCheckRule
 */
@Data
@NoArgsConstructor
public class ItemCheckRule implements RuleStrategy<Map<String, Integer>> {
    @Override
    public String getRuleType() {
        return "ITEM_CHECK";
    }

    @Override
    public boolean validate(RuleContext context) {
        return context.getParam("requiredItems") != null;
    }

    @Override
    @SuppressWarnings("unchecked")
    public RuleResult<Map<String, Integer>> execute(RuleContext context) {
        Map<String, Integer> requiredItems = context.getParam("requiredItems", Map.class);
        Map<String, Integer> playerItems = context.getParam("playerItems", Map.class);

        if (playerItems == null) {
            playerItems = new HashMap<>();
        }

        Map<String, Integer> missingItems = new HashMap<>();

        for (Map.Entry<String, Integer> entry : requiredItems.entrySet()) {
            String itemId = entry.getKey();
            int requiredCount = entry.getValue();
            int playerCount = playerItems.getOrDefault(itemId, 0);

            if (playerCount < requiredCount) {
                missingItems.put(itemId, requiredCount - playerCount);
            }
        }

        if (missingItems.isEmpty()) {
            return RuleResult.success("物品条件满足", new HashMap<>());
        } else {
            return RuleResult.success("缺少所需物品", missingItems);
        }
    }
}
